package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.ProductService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Controller
@RequestMapping("/product")
public class ProductController {

    public final String uri = "E:/upload";

    @Autowired
    ProductService productService;

    /**
     * 产品前端分页
     * @param search
     * @param categoryName
     * @return
     */
    @RequestMapping("/product-list")
    @ResponseBody
    public List<Productt> selectProduct(String search,String categoryName){
         return productService.selectProduct(search,categoryName);
    }



    /*@GetMapping("upload/**")
    @ResponseBody
    public void upload(HttpServletRequest request,
                       HttpServletResponse response) {
        System.out.println(imgs);
        String servletPath = request.getServletPath();
        String path = uri + "/" + servletPath.replaceFirst("/upload/","");
        System.out.println(path);
        //判断
        File file = new File(path);
        if(!file.exists()) {
            //抛出异常
            try {
                response.sendError(404, "文件路径不存在");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            //使用字节流把图片从本地盘中读取到内存
            InputStream is = new FileInputStream(file);
            //servlet中的流将图片传回客户端显示
            ServletOutputStream s = response.getOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            //循环读取
            while((len = is.read(b,0,len))!= -1) {
                s.write(b, 0, len);
            }
            s.close();
            is.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
*/
    /**
     * 新增产品
     * @param productArray
     */
    @PostMapping("/addProduct")
    @ResponseBody
    public String addProduct(@RequestParam(value = "file",required = false)
                           MultipartFile file, @RequestParam("productArray") String productArray){
        String imgs = null;
        System.out.println(file);
        JSONObject obj = (JSONObject)JSONObject.parse(productArray);
        System.out.println(obj);
        Product product = obj.getObject("product",Product.class);
        Productt productt = obj.getObject("productt",Productt.class);
        List<Integer> guige = obj.getObject("guige",List.class);
        List<Integer> attrid = obj.getObject("AttrList",List.class);
        if(file != null){
            String fileName = file.getOriginalFilename();
            String ext = FilenameUtils.getExtension(fileName);
            //生成一个新的名字
            String newName = UUID.randomUUID().toString();
            String filePath = newName + "." + ext;
            try {
                File f = new File(uri,filePath);
                if(!f.exists()) {
                    f.mkdirs();//如果目录不存在，创建
                }
                //文件转移
                file.transferTo(f);
                //将路径存进数据库
                imgs = "upload/" + filePath;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        productService.addProduct(product,productt,guige,attrid,imgs);
        //回显路径
        return  imgs;

    }

    /**
     * 查询需要修改的产品
     * @param productId
     * @return
     */
    @RequestMapping("/upProduct")
    @ResponseBody
    public List<Productt> upProduct(Integer productId){
       return productService.upProduct(productId);
    }

    /**
     * 根据产品id删除产品
     * @param producttId
     */
    @RequestMapping("/delProduct")
    @ResponseBody
    public void delProduct(Integer producttId){
        productService.delProduct(producttId);
    }

    /**
     * 查询类别集合
     * @return
     */
    @RequestMapping("/category-list")
    @ResponseBody
    public List<Category> selectCategory(){
        return productService.selectCategory();
    }

    /**
     * 根据产品id查询属性名
     * @param productId
     * @return
     */
    @RequestMapping("/selectchanpinshuxin")
    @ResponseBody
    public List<Attribute> selectchanpinshuxin(Integer productId){
        return productService.selectchanpinshuxin(productId);
    }

    /**
     *查询类别是否被产品使用
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectProCategory")
    @ResponseBody
    public List<Product> selectProCategory(Integer categoryId){
        return productService.selectProCategory(categoryId);
    }


    /**
     * 产品新增判断
     * @param productName
     * @return
     */
    @RequestMapping("/selectProAtv")
    @ResponseBody
    public List<Productt> selectProAtv(String productName,Integer producttId){
        return productService.selectProAtv(productName,producttId);
    }

    /**
     * 产品新增判断
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectProAtName")
    @ResponseBody
    public List<Attribute> selectProAtName(Integer categoryId){
        return productService.selectProAtName(categoryId);
    }

    /**
     * 产品新增判断
     * @param categoryId
     * @return
     */
    @RequestMapping("/selectAtvName")
    @ResponseBody
    public List<AttributeValue> selectProAtvName(Integer categoryId){
        return productService.selectAtvName(categoryId);
    }

    /**
     * 产品筛选分页
     * @param search
     * @param categoryName
     * @param price1
     * @param price2
     * @return
     */
    @RequestMapping("/productScreen")
    @ResponseBody
    public List<Productt> selectScreen(String search,String categoryName,String price1,String price2){
        return productService.selectScreen(search,categoryName,price1,price2);
    }

}
